package com.sjlh.centurylianhua.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sjlh.centurylianhua.dto.SearchCon;
import com.sjlh.centurylianhua.entity.Inventory;
import com.sjlh.centurylianhua.mapper.InventoryMapper;
import com.sjlh.centurylianhua.utils.ResultData;
import com.sjlh.centurylianhua.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
@RequestMapping("/inventory")
@CrossOrigin
public class InventoryController {

    @Autowired
    private InventoryMapper inventoryMapper;

    @RequestMapping("/all")
    public ResultData getAll(@RequestBody SearchCon sc){
//      分页对象，传入当前页码及每页的数量
        QueryWrapper<Inventory> wra=new QueryWrapper<Inventory>();
        wra.eq(sc.getTypeName2()!=null,"inve.depotId",sc.getTypeName2());
        wra.eq(sc.getTypeName3()!=null,"inve.productId",sc.getTypeName3());
        wra.apply("product.id=inve.productId and depot.id=inve.depotId");
        //封装分页对象
        if(sc.getPage()!=null) {
            Page<Inventory> pa=new Page<Inventory>(sc.getPage(),sc.getLimit());
            List<Inventory> uspa=inventoryMapper.inventorylist2(pa,wra);
            return new ResultData(true,20000," " ,uspa,pa.getTotal());
        }else{
            long count= inventoryMapper.inventorylist1(wra).size();
            return new ResultData(true,20000," " ,inventoryMapper.inventorylist1(wra),count);
        }
    }

    /**
     *
     * */
    @RequestMapping("/set")
    public Object set(@RequestBody Inventory inventory){
        UpdateWrapper<Inventory> updateWrapper = new UpdateWrapper<Inventory>();
        updateWrapper.set(StringUtils.isnotNullObj(inventory.getTopXian()),"topXian",inventory.getTopXian());
        updateWrapper.set(StringUtils.isnotNullObj(inventory.getDownXian()),"downXian",inventory.getDownXian());
        updateWrapper.eq("id",inventory.getId());
        int r = inventoryMapper.update(null,updateWrapper);
            if(r==1)
                return new ResultData(true,20000,"修改模块成功",true,null);
            else
                return new ResultData(false,20000,"修改模块失败",false,null);
    }
}
